﻿using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEditor.VersionControl;
using UnityEngine;

namespace EditorExtension
{
    public class AssetModificationProcessorExample : AssetModificationProcessor
    {
        // 创建资产时的回调
        private static void OnWillCreateAsset(string assetName)
        {
            Debug.Log($"OnWillCreateAsset {assetName}");
        }
        
        // 删除资产时的回调
        private static AssetDeleteResult OnWillDeleteAsset(string assetPath, RemoveAssetOptions options)
        {
            Debug.Log($"OnWillDeleteAsset {assetPath} {options}");

            if (EditorUtility.DisplayDialog("删除资源", "确定要删除吗？", "确定", "取消"))
            {
                return AssetDeleteResult.DidNotDelete; // 扔进回收站
            }
            else
            {
                return AssetDeleteResult.FailedDelete; // 删除失败
            }
        }

        // 移动资产时的回调
        private static AssetMoveResult OnWillMoveAsset(string sourcePath, string destinationPath)
        {
            Debug.Log($"OnWillMoveAsset {sourcePath} => {destinationPath}");

            if (EditorUtility.DisplayDialog("移动资源", "确定要移动吗？", "确定", "取消"))
            {
                return AssetMoveResult.DidMove;
            }
            else
            {
                return AssetMoveResult.FailedMove; // 移动失败
            }
        }

        // 保存资源时的回调
        private static string[] OnWillSaveAssets(string[] paths)
        {
            Debug.Log($"OnWillSaveAssets {paths}");
            return paths;
        }
        
        // 资源打开编辑时的回调
        private static bool CanOpenForEdit(string[] assetOrMetaFilePaths, List<string> outNotEditablePaths,
            StatusQueryOptions statusQueryOptions)
        {
            Debug.Log("CanOpenForEdit");
            return true;
        }
        
        // 文件模式变更时的回调
        private static void FileModeChanged(string[] paths, FileMode mode)
        {
            Debug.Log($"FileModeChanged {mode}");

            foreach (string path in paths)
            {
                Debug.Log(path);
            }
        }
        
        // 设置可编辑时的回调
        private static bool MakeEditable(string[] paths, string prompt, List<string> outNotEditablePaths)
        {
            Debug.Log("MakeEditable");
            foreach (string path in paths)
            {
                Debug.Log(path);
            }

            return true;
        }
    }
}